package com.wbk.lotterysystemlotteryservice.service.serviceImpl;


import com.wbk.lotterysystemlotteryservice.entity.Record;
import com.wbk.lotterysystemlotteryservice.entity.RecordExample;
import com.wbk.lotterysystemlotteryservice.mapper.RecordMapper;
import com.wbk.lotterysystemlotteryservice.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    RecordMapper recordMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int create(Record record) {
        record.setLotteryResult((byte) 0);
        record.setCreateTime(new Date());
        return recordMapper.insertSelective(record);
    }

    @Override
    public List<Record> getAllWithLottery(Integer lotteryId) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andLotteryIdEqualTo(lotteryId);
        return recordMapper.selectByExample(recordExample);
    }

    @Override
    public Boolean alreadyJoin(Integer lotteryId, Long userNo) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andLotteryIdEqualTo(lotteryId);
        criteria.andUserNoEqualTo(userNo);
        return recordMapper.countByExample(recordExample) == 0;
    }

    @Cacheable(cacheNames = "recordWin::byLotteryId", key = "#lotteryId")
    @Override
    public List<Record> getWinner(Integer lotteryId) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andLotteryIdEqualTo(lotteryId);
        criteria.andLotteryResultEqualTo((byte) 1);
        return recordMapper.selectByExample(recordExample);
    }

    @Override
    public List<Record> hasJoin(Long userNo) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andUserNoEqualTo(userNo);
        return recordMapper.selectByExample(recordExample);
    }

    @Cacheable(cacheNames = "record::query", key = "#lotteryId+'-'+#userNo")
    @Override
    public Record query(Integer lotteryId, Long userNo) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andLotteryIdEqualTo(lotteryId);
        criteria.andUserNoEqualTo(userNo);
        List<Record> records = recordMapper.selectByExample(recordExample);
        if (records.isEmpty()) {
            return null;
        }
        return records.get(0);
    }

    @CacheEvict(cacheNames = "record::query", key = "#record.lotteryId+'-'+#record.userNo")
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int update(Record record) {
        return recordMapper.updateByPrimaryKeySelective(record);
    }


}
